var MAX_SEARCH_TIME = 10000; //搜索控件时超时时间 http://doc.autoxjs.com/#/widgetsBasedAutomation?id=uiselectorfindonetimeout

function bezier_curves(cp, t) {
  cx = 3.0 * (cp[1].x - cp[0].x);
  bx = 3.0 * (cp[2].x - cp[1].x) - cx;
  ax = cp[3].x - cp[0].x - cx - bx;
  cy = 3.0 * (cp[1].y - cp[0].y);
  by = 3.0 * (cp[2].y - cp[1].y) - cy;
  ay = cp[3].y - cp[0].y - cy - by;

  tSquared = t * t;
  tCubed = tSquared * t;
  result = {
    x: 0,
    y: 0,
  };
  result.x = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
  result.y = ay * tCubed + by * tSquared + cy * t + cp[0].y;
  return result;
}

//仿真随机带曲线滑动
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function sml_move(qx, qy, zx, zy, time) {
  var xxy = [time];
  var point = [];
  var dx0 = {
    x: qx,
    y: qy,
  };

  var dx1 = {
    x: random(qx - 100, qx + 100),
    y: random(qy, qy + 50),
  };
  var dx2 = {
    x: random(zx - 100, zx + 100),
    y: random(zy, zy + 50),
  };
  var dx3 = {
    x: zx,
    y: zy,
  };
  point.push(dx0);
  point.push(dx1);
  point.push(dx2);
  point.push(dx3);

  for (let i = 0; i < 1; i += 0.08) {
    xxyy = [
      parseInt(bezier_curves(point, i).x),
      parseInt(bezier_curves(point, i).y),
    ];

    xxy.push(xxyy);
  }

  gesture.apply(null, xxy);
}
var utils = {
  click_ocr_text(ocr_text) {
    this.click_bounds(ocr_text.bounds);
  },
  click_ui_control(ui_control) {
    //点击ui控件
    while (true) {
      if (ui_control && ui_control.clickable()) {
        ui_control.click();
        return true;
      } else if (ui_control) {
        ui_control = ui_control.parent();
      } else {
        return false;
      }
    }
  },
  click_bounds(bounds) {
    return this.click_x_y(bounds.centerX(), bounds.centerY());
  },
  exists_text(content) {
    if (text(content).exists()) {
      return text(content).findOne(MAX_SEARCH_TIME);
    } else {
      return false;
    }
  },
  click_text(content) {
    if (this.exists_text(content)) {
      var ui_text = text(content).findOne(MAX_SEARCH_TIME);
      var bounds = ui_text.bounds();
      this.click_bounds(bounds);
      return ui_text;
    } else {
      return false;
    }
  },
  exists_text_starts_with(content) {
    if (textStartsWith(content).exists()) {
      return textStartsWith(content).findOne(MAX_SEARCH_TIME);
    } else {
      return false;
    }
  },
  click_text_starts_with(content) {
    if (this.exists_text_starts_with(content)) {
      var ui_text = textStartsWith(content).findOne(MAX_SEARCH_TIME);
      var bounds = ui_text.bounds();
      this.click_bounds(bounds);
      return ui_text;
    } else {
      return false;
    }
  },
  exists_text_ends_with(content) {
    if (textEndsWith(content).exists()) {
      return textEndsWith(content).findOne(MAX_SEARCH_TIME);
    } else {
      return false;
    }
  },
  click_text_ends_with(content) {
    if (this.exists_text_ends_with(content)) {
      var ui_text = textEndsWith(content).findOne(MAX_SEARCH_TIME);
      var bounds = ui_text.bounds();
      this.click_bounds(bounds);
      return ui_text;
    } else {
      return false;
    }
  },
  exists_text_matches(content) {
    if (textMatches(content).exists()) {
      return textMatches(content).findOne(MAX_SEARCH_TIME);
    } else {
      return false;
    }
  },
  click_text_matches(content) {
    if (this.exists_text_matches(content)) {
      var ui_text = textMatches(content).findOne(MAX_SEARCH_TIME);
      var bounds = ui_text.bounds();
      this.click_bounds(bounds);
      return ui_text;
    } else {
      return false;
    }
  },
  get_center_point(x1, y1, x2, y2) {
    return {
      x: parseInt(x1 + (x2 - x1) / 2),
      y: parseInt(y1 + (y2 - y1) / 2),
    };
  },
  click_point(x1, y1, x2, y2) {
    var point = this.get_center_point(x1, y1, x2, y2);
    return this.click_x_y(point.x, point.y);
  },
  click_x_y(x, y) {
    return click(x, y);
  },
  get_number(content) {
    var reg_exp = /(\d+)/g;
    var number_match = content.match(reg_exp);
    var number = parseInt(number_match ? number_match[0] : 0);
    return number;
  },
  get_size_point(points, w, h) {
    var length = points.length;
    var size_points = [];
    var rate_x = device.width / (w || 720);
    var rate_y = device.height / (h || 1650);
    for (var i = 0; i < length; i++) {
      var point = points[i];
      var rate = i % 2 == 0 ? rate_x : rate_y;
      size_points.push(parseInt(point * rate));
    }
    return size_points;
  },
  get_random_interval(base, start, end, times) {
    start = start || 0;
    end = end || 0;
    times = times || 1000;
    return (base + random(start, end)) * times;
  },
  get_swipe_times(start, end) {
    start = start || 95;
    end = end || 5;
    return (start + random(0, end)) / 100;
  },
  create_logger() {
    var params = [];
    var length = arguments.length;
    for (var i = 0; i < length; i++) {
      params.push(arguments[i]);
    }
    return function () {
      var params_log = [];
      var length = arguments.length;
      for (var i = 0; i < length; i++) {
        params_log.push(arguments[i]);
      }
      console.log.apply(console.log, params.concat(params_log));
    };
  },
  random_index_array(max) {
    var result = [];
    while (result.length < max) {
      var number = random(0, max - 1);
      if (result.indexOf(number) == -1) {
        result.push(number);
      }
    }
    return result;
  },
  swipe(a, b, c, d) {
    swipe(
      device.width * a * this.get_swipe_times(95, 5),
      device.height * b * this.get_swipe_times(95, 5),
      device.width * c * this.get_swipe_times(95, 5),
      device.height * d * this.get_swipe_times(95, 5),
      utils.get_random_interval(200, 0, 800, 1)
    );
  },
  str_format(str, str2) {
    if (!str) {
      return "";
    }
    str2 = str2 || "哈";
    return str.split("").join(str2);
  },
  swipe_x(qx, qy, zx, zy, time) {
    sml_move(qx, qy, zx, zy, time);
  },
};

module.exports = utils;
